package me.jayjayjay.saboteur;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsCharacter;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.MaterialList;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.plugins.ogre.OgreMeshKey;


class Saboteur extends SimpleApplication implements ActionListener {

    BulletAppState bulletAppState;
    private Node gameLevel;
    private PhysicsCharacter player;
    private Vector3f walkDirection = new Vector3f();
    private boolean left, right, up ,down, q;

    
    public static void main(String[] args) {
        Saboteur main = new Saboteur();
        main.start();
    }
    
    @Override
    public void simpleInitApp() {       
        assetManager.registerLocator("quake3level.zip", ZipLocator.class);
        
        initLight();
        initLevel();
        initPhysics();
        initHud();
        initKeys();

        player = new PhysicsCharacter(new SphereCollisionShape(5), .01f);
        player.setJumpSpeed(40);
        player.setFallSpeed(50);
        player.setGravity(150);
        
        player.setPhysicsLocation(new Vector3f(60, 10, -60));

        bulletAppState.getPhysicsSpace().addAll(gameLevel);
        bulletAppState.getPhysicsSpace().add(player);
        
    }

    private void initLight() {
        //Gerades Licht
        DirectionalLight dl = new DirectionalLight();
        dl.setColor(ColorRGBA.White.clone().multLocal(2));
        dl.setDirection(new Vector3f(-1, -1, -1).normalize());
        rootNode.addLight(dl);

        //Umgebungs Licht
        AmbientLight am = new AmbientLight();
        am.setColor(ColorRGBA.White.mult(2));
        rootNode.addLight(am);


    }

    private void initLevel() {
        MaterialList matList = (MaterialList) assetManager.loadAsset("Scene.material");
        OgreMeshKey key = new OgreMeshKey("main.meshxml", matList);
        gameLevel = (Node) assetManager.loadAsset(key);
        gameLevel.setLocalScale(0.1f);
        
        rootNode.attachChild(gameLevel);
    }

    private void initPhysics() {
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        
        gameLevel.addControl(new RigidBodyControl(0));
    }

    @Override
    public void simpleUpdate(float tpf) {
        Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
        Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
        
        //Reset
        walkDirection.set(0,0,0);
        
        if(left)
            walkDirection.addLocal(camLeft.setY(0));
        if(right)
            walkDirection.addLocal(camLeft.negate().setY(0));
        if(up)
            walkDirection.addLocal(camDir.setY(0));
        if(down)
            walkDirection.addLocal(camDir.negate().setY(0));
        if(q)
            walkDirection.addLocal(new Vector3f(0,300*tpf,0));
        

        player.setWalkDirection(walkDirection);
        cam.setLocation(player.getPhysicsLocation());
     }

    private void initHud() {
        //TODO
    }

    private void initKeys() {
        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("Fly", new KeyTrigger(KeyInput.KEY_Q));
        inputManager.addListener(this, "Left");
        inputManager.addListener(this, "Right");
        inputManager.addListener(this, "Up");
        inputManager.addListener(this, "Down");
        inputManager.addListener(this, "Space");
        inputManager.addListener(this, "Fly");
    }
    
    public void onAction(String name, boolean isPressed, float tpf) {
         if(name.equals("Left"))  
            if(isPressed)
                left = true;
            else
                left = false;
         
         if(name.equals("Right"))  
            if(isPressed)
                right = true;
            else
                right = false;
         
         if(name.equals("Up"))  
            if(isPressed)
                up = true;
            else
                up = false;
         
         if(name.equals("Down"))  
            if(isPressed)
                down = true;
            else
                down = false;
        
        
        
        if(name.equals("Space"))
            player.jump();
        
        
        if(name.equals("Fly"))  
            if(isPressed)
                q = true;
            else
                q = false;
        
    }
    
}